﻿// <copyright file="VoiceCommand.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace KinectExtensions.Nui
{
    #region

    using System;
    using System.Windows.Input;

    #endregion

    public class VoiceCommand : IVoiceCommand, ICommand
    {
        #region Constants and Fields

        private readonly Action<object, float> callbackWithParam;

        private readonly Func<object, bool> canExecuteWithParam;

        #endregion

        #region Constructors and Destructors

        public VoiceCommand(Action callback, string word, string barHintText = null, Func<bool> canExecute = null)
        {
            this.callbackWithParam = (prm, confidence) => callback();
            this.Word = word;
            this.BarHintText = barHintText;

            if (canExecute != null)
            {
                this.canExecuteWithParam = prm => canExecute();
            }
        }

        public VoiceCommand(Action<object> callback, string word, string barHintText = null, Func<object, bool> canExecute = null)
        {
            this.Word = word;
            this.BarHintText = barHintText;
            this.callbackWithParam = (prm, confidence) => callback(prm);
            this.canExecuteWithParam = canExecute;
        }

        public VoiceCommand(Action<object, float> callback, string word, string barHintText = null, Func<object, bool> canExecute = null)
        {
            this.Word = word;
            this.BarHintText = barHintText;
            this.callbackWithParam = callback;
            this.canExecuteWithParam = canExecute;
        }

        #endregion

        #region Public Events

        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
            }
            remove
            {
                CommandManager.RequerySuggested -= value;
            }
        }

        #endregion

        #region Public Properties

        public string BarHintText { get; private set; }

        public string Word { get; private set; }

        #endregion

        #region Public Methods

        public bool CanExecute(object parameter)
        {
            if (this.canExecuteWithParam != null)
            {
                return this.canExecuteWithParam(parameter);
            }

            return true;
        }

        public void Execute(object parameter, float confidence)
        {
            this.callbackWithParam(parameter, confidence);
        }

        public void Execute(object parameter)
        {
            this.callbackWithParam(parameter, -1);
        }

        #endregion
    }
}